<?php
namespace app\admin\model;

use think\Model;
use think\Db;

class TeacherModel extends Model{
	// 确定链接表名
    protected $name = 'teacher';

    /**
	 * [获取信息]
	 */
	public function getInfoByWhere($where='',$whereOr='',$is_page=false,$offset=0,$limit=20,$order='id desc'){
		if($is_page){
			return $this->where($where)->where($whereOr)->where($this->whereCansee())->limit($offset, $limit)->order($order)->select();
		}else{
			return $this->where($where)->where($whereOr)->where($this->whereCansee())->count();
		}
	}

	/**
	 * [添加教务老师]
	 */
	public function addTeacher($param){
		try{
            $result = $this->validate('TeacherValidate')->save($param);
            if(false === $result){
                return ['code'=>0,'msg'=>'添加失败:'.$this->getError()];
            }else{
                return ['code'=>1,'msg'=>'添加成功'];
            }
        }catch(Exception $e){
            return ['code'=>0,'msg'=>'添加失败'.$e->getMessage()];
        }
	}

	/**
	 * [编辑教务老师]
	 */
	public function editTeacher($param){
		try{
            $result = $this->validate('TeacherValidate')->isUpdate(true)->save($param);
            if(false === $result){
                return ['code'=>0,'msg'=>'编辑失败:'.$this->getError()];
            }else{
                return ['code'=>1,'msg'=>'编辑成功'];
            }
        }catch(Exception $e){
            return ['code'=>0,'msg'=>'编辑失败'.$e->getMessage()];
        }
	}

	/**
     * [删除教务老师]
     */
    public function delTeacher($idArr){
        $this::startTrans();
        try{
            foreach($idArr as $v){
                $this->where('id',$v)->delete();
            }
            $this::commit();
            return ['code'=>1,'msg'=>'删除成功'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"删除失败：".$e->getMessage()];
        }
    }

    public function delTeacherUser($param){
    	$this::startTrans();
        try{
            $this->where('id',$param)->update(['user_id'=>0]);
            $this::commit();
            return ['code'=>1,'msg'=>'删除成功'];
        }catch(Exception $e){
            $this::rollback();
            return ['code'=>0,'msg'=>"删除失败：".$e->getMessage()];
        }
    }
}